package com.mapbar.pay.dao.impl;

import com.mapbar.pay.dao.DeviceDao;
import com.mapbar.pay.entity.ActivationState;
import com.mapbar.pay.entity.Device;
import com.mapbar.pay.util.ProjectUtil;
import com.mapbar.pay.util.Util;
import java.util.HashMap;
import java.util.Map;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;

/**
 *@Date 创建时间 2010-03-05 10:17:52
 *@author fengbj
 * 
 */
public class DeviceDaoImpl implements DeviceDao {

    public Logger logger = Logger.getLogger(this.getClass());//日志对象

    /**
     * 校验序列号
     * @param serial
     * @return
     */
    public boolean checkSerial(String serial) {

        if (serial != null) {
            //：&serial_num=HG7R-X7HY-SG2N-KJP2&type=isSerial_num,返回值{isSerial_num:"0/1"}
            //组装参数
            Map<String, String> map = new HashMap<String, String>();
            map.put("serial_num", serial);
            map.put("type", "isSerial_num");

            //请求接口
            String jsonString = Util.getSrc(Util.getUrl(ProjectUtil.CHECK_SERIAL), map);
            JSONObject json = Util.initJSONObject(jsonString);

            if (json != null) {

                try {
                    if ("0".equals(json.getString("isSerial_num"))) {
                        logger.info("序列号校验成功！serial[" + serial + "]");

                        return true;
                    } else {

                        logger.info("序列号校验失败，不是合法序列号！serial[" + serial + "]");
                        return false;
                    }

                } catch (Exception e) {
                    logger.error("序列号校验异常！serial[" + serial + "] " + e.toString());
                    return false;
                } finally {
                    //destory
                    jsonString = null;
                    json = null;
                }

            } else {
                logger.error("校验序列号时接口[" + Util.getUrl(ProjectUtil.CHECK_SERIAL) + "] 返回为空 ， serial = [" + serial + "]");
                return false;
            }

        } else {
            logger.error("空序列号不执行校验步骤");
            return false;
        }

    }

    /**
     * 校验设备码
     * @param deviceId
     * @return
     */
    public boolean checkDeviceId(String deviceId) {


        if (deviceId != null) {
            //：:&deviceid=HG7R-X7HY-SG6N-KJP3&type=isDeviceid,返回值{isDeviceid:"0/1"}
            //组装参数
            Map<String, String> map = new HashMap<String, String>();
            map.put("deviceid", deviceId);
            map.put("type", "isDeviceid");

            //请求接口
            String jsonString = Util.getSrc(Util.getUrl(ProjectUtil.CHECK_DEVICE_ID), map);
            JSONObject json = Util.initJSONObject(jsonString);

            if (json != null) {

                try {
                    if ("0".equals(json.getString("isDeviceid"))) {
                        logger.info("设备码校验成功！deviceId[" + deviceId + "]");

                        return true;
                    } else {

                        logger.info("设备码校验失败，不是合法序列号！deviceId[" + deviceId + "]");
                        return false;
                    }

                } catch (Exception e) {
                    logger.error("设备码校验异常！deviceId[" + deviceId + "] " + e.toString());
                    return false;
                } finally {
                    //destory
                    jsonString = null;
                    json = null;
                }

            } else {
                logger.error("校验设备码时接口[" + Util.getUrl(ProjectUtil.CHECK_DEVICE_ID) + "] 返回为空 ， deviceId = [" + deviceId + "]");
                return false;
            }

        } else {
            logger.error("空设备码不执行校验步骤");
            return false;
        }
    }

    /**
     * 生成激活码
     * @param serial 序列号
     * @param deviceId 设备码
     * @return ActivationState 激活码的状态信息
     */
    public ActivationState createActivation(String serial, String deviceId) {

        if (serial != null && deviceId != null) {
            //:&deviceid=HG7R-X7HY-SG6N-KJP3&serial_num=HG7R-X7HY-SG2N-KJP2&type=createActiveCode,返回值{isSerial_num:"0/1"}
            //组装参数
            Map<String, String> map = new HashMap<String, String>();
            map.put("serial_num", serial);
            map.put("deviceid", deviceId);
            map.put("type", "createActiveCode");

            //请求接口
            String jsonString = Util.getSrc(Util.getUrl(ProjectUtil.GET_ACTIVATION_INFO_URL), map);
            JSONObject json = Util.initJSONObject(jsonString);

            if (json != null) {

                try {
                    ActivationState activationState = new ActivationState();

                    //序列号
                    if ("0".equals(json.getString("isSerial_num"))) {
                        activationState.setIsSerial(true);
                    } else {
                        activationState.setIsSerial(false);
                    }

                    //设备码
                    if ("0".equals(json.getString("isDeviceid"))) {
                        activationState.setIsDeviceId(true);
                    } else {
                        activationState.setIsDeviceId(false);
                    }

                    //设备码
                    if ("0".equals(json.getString("isDeviceid"))) {
                        activationState.setIsDeviceId(true);
                    } else {
                        activationState.setIsDeviceId(false);
                    }

                    //激活码
                    if ("0".equals(json.getString("isActive"))) {
                        activationState.setIsActive(true);
                    } else {
                        activationState.setIsActive(false);
                    }

                    //属性
                    activationState.setActivation(json.getString("activeCode"));
                    activationState.setExpiryDate(json.getString("expiry_date"));
                    activationState.setActivecodeDate(json.getString("activecode_date"));

                    logger.info("获取激活码完成：[" + activationState + "]");
                    return activationState;

                } catch (Exception e) {
                    logger.error("获取激活码异常！serial[" + serial + ",deviceId =" + deviceId + "] " + e.toString());
                    return null;
                } finally {
                    //destory
                    jsonString = null;
                    json = null;
                }

            } else {
                logger.error("获取激活码时接口[" + Util.getUrl(ProjectUtil.GET_ACTIVATION_INFO_URL) + "] 返回为空 ， serial = [" + serial + ",deviceId =" + deviceId + "]");
                return null;
            }

        } else {
            logger.error("空序列号或设备码不执行获取激活码步骤");
            return null;
        }
    }

    /**
     * 获取设备信息
     * @param serial
     * @return
     */
    public Device getDeviceInfo(String serial) {

        if (serial != null) {
            //&serial_num=HG7R-X7HY-SG2N-KJP2&type=getSerialInfo
            //组装参数
            Map<String, String> map = new HashMap<String, String>();
            map.put("serial_num", serial);
            map.put("type", "getSerialInfo");

            //请求接口
            String jsonString = Util.getSrc(Util.getUrl(ProjectUtil.GET_DEVICE_INFO_URL), map);
            logger.info("接口[" + Util.getUrl(ProjectUtil.GET_DEVICE_INFO_URL) + "]返回信息" + jsonString);
            JSONObject json = Util.initJSONObject(jsonString);

            if (json != null) {

                try {
                    //ActivationState activationState = new ActivationState();
                    Device device = new Device();
                    //序列号
                    if ("0".equals(json.getString("isSerial_num"))) {
                        device.setIsSerial(true);
                    } else {
                        device.setIsSerial(false);
                    }

                    //激活码
                    if ("0".equals(json.getString("isActive"))) {
                        device.setIsActive(true);
                    } else {
                        device.setIsActive(false);
                    }

                    //属性
                    device.setActivation(json.getString("activeCode"));
                    device.setDeviceId(json.getString("deviceid"));
                    device.setDeviceCorpName(json.getString("device_corp_name"));
                    device.setBrand(json.getString("brand"));
                    device.setDeviceModel(json.getString("device_model"));
                    device.setActivecodeDate(json.getString("activecode_date"));
                    device.setExpiryDate(json.getString("expiry_date"));

                    logger.info("获取设备信息完成：[" + device + "]");
                    return device;

                } catch (Exception e) {
                    logger.error("获取设备信息异常！serial[" + serial + "] " + e.toString());
                    return null;
                } finally {
                    //destory
                    jsonString = null;
                    json = null;
                }

            } else {
                logger.error("获取激活码时接口[" + Util.getUrl(ProjectUtil.GET_DEVICE_INFO_URL) + "] 返回为空 ， serial = [" + serial + "]");
                return null;
            }

        } else {
            logger.error("空序列号不执行获取设备信息的步骤");
            return null;
        }

    }

    @Override
    public String getCrop(String type) {
        if (type == null) {
            return null;
        } else {

            HashMap<String, String> map = new HashMap<String, String>();
            map.put("device_type", type);
            try {
                return Util.getSrc(Util.getUrl(ProjectUtil.GET_CORP), map);
            } catch (Exception e) {
                logger.error("获取厂商信息异常，type=" + type + "," + e.toString());
                return null;
            } finally {
                map = null;
            }


        }

    }

    public String getBrandsByCorpId(int corpId) {

        HashMap<String, String> map = new HashMap<String, String>();
        map.put("device_corp_id", corpId + "");
        try {
            return Util.getSrc(Util.getUrl(ProjectUtil.GET_BRANDS_BY_CORPID), map);
        } catch (Exception e) {
            logger.error("获取厂商信息异常，type=" + corpId + "," + e.toString());
            return null;
        } finally {
            map = null;
        }


    }

    @Override
    public String getDeviceModel(int brandId) {

        HashMap<String, String> map = new HashMap<String, String>();
        map.put("device_brand_id", brandId + "");
        try {
            return Util.getSrc(Util.getUrl(ProjectUtil.GET_DEVICE_MODEL), map);
        } catch (Exception e) {
            logger.error("获取型号信息异常，type=" + brandId + "," + e.toString());
            return null;
        } finally {
            map = null;
        }
    }
}
